using System.Web;
using System.Web.Routing;
using Limestone;

namespace LimestoneMVC.Controllers
{
    public class PagePathConstraint : IRouteConstraint
    {
        public IPageStubCache PageStubCache { get; set; }

        public PagePathConstraint(IPageStubCache pageStubCache)
        {
            PageStubCache = pageStubCache;
        }

        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            var pagePath = values[parameterName] as string;
            if (string.IsNullOrEmpty(pagePath))
            {
                pagePath = "/";
            }
            else
            {
                if(!pagePath.StartsWith("/"))
                    pagePath = "/" + pagePath;
            }

            return PageStubCache.GetPageStub(pagePath) != null;
        }
    }
}